package com.example.teatea.dao;

import com.example.teatea.project.Good;
import com.example.teatea.project.Staff;
import com.example.teatea.tools.GetTime;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.HashMap;
import java.util.Properties;

public class AddMenu {
    public void addMenu(HashMap<Good,Integer> map, Staff staff,int seat){
        Connection connection = null;
        ResultSet resultSet = null;
        Properties info = new Properties();
        try {
            info.load(new FileInputStream("src/main/resources/com/example/teatea/sql.properties"));
            String user = info.getProperty("user");
            String password = info.getProperty("password");
            String Driver = info.getProperty("Driver");
            String url = info.getProperty("url");
            Class.forName(Driver);
            connection = DriverManager.getConnection(url,user,password);
            String sql1 = "insert into menu(mType,mTime,mStaff)values(?,?,?)";
            String sql2 = "insert into goodmenu(mId,gName,gNum)values(?,?,?)";
            String sql3 = "update seat set rState = 1,rMenu = ? where rId = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql1);
            preparedStatement.setInt(1,0);
            preparedStatement.setString(2,new GetTime().getTime());
            preparedStatement.setString(3,staff.getsName());
            preparedStatement.executeUpdate();
            preparedStatement = connection.prepareStatement(sql2);
            for(Good s: map.keySet()){
                preparedStatement.setInt(1,new GetMenuNum().getMenuNum());
                preparedStatement.setString(2,s.getgName());
                preparedStatement.setInt(3,map.get(s));
                preparedStatement.executeUpdate();
            }
            preparedStatement = connection.prepareStatement(sql3);
            preparedStatement.setInt(1,new GetMenuNum().getMenuNum());
            preparedStatement.setInt(2,seat);
            preparedStatement.executeUpdate();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
